package com.epoint.flinkdemo.transform;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author liufl
 * @version [版本号, 21-4-8]
 */
public class FlatMapTest
{
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        System.out.println("默认并行度=====>" + env.getParallelism());
        String[] words = {"apple","orange","banana","watermelon"};

        // 创建DataSource
        DataStreamSource<String> streamSource = env.fromElements(words);
        DataStream<String> flatStream = streamSource
                .flatMap(new FlatMapFunction<String, String>()
                {
                    @Override
                    public void flatMap(String value, Collector<String> out) throws Exception {
                        // out收集多个元素
                        out.collect(value);
                        out.collect(value.toUpperCase());
                    }
                }).setParallelism(2);
        flatStream.print();

        env.execute();
    }
}
